package com.cvo.scrumtoolkit.client.pannels;

import com.cvo.scrumtoolkit.client.controller.ControllerImpl;
import com.cvo.scrumtoolkit.client.controller.Navigator;
import com.cvo.scrumtoolkit.client.controller.View;
import com.cvo.scrumtoolkit.client.entities.BacklogItem;
import com.cvo.scrumtoolkit.client.entities.Project;
import com.cvo.scrumtoolkit.client.model.ProjectStore;
import com.cvo.scrumtoolkit.client.pannels.widgets.BacklogWidget;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class BacklogPanel extends Composite {
	
	private Project project;
	private static BacklogPanelUiBinder uiBinder = GWT
			.create(BacklogPanelUiBinder.class);
	
			
	interface BacklogPanelUiBinder extends UiBinder<Widget, BacklogPanel> {
	}
	@UiField 
	VerticalPanel verticalPanel;
	@UiField
	TextBox txtBacklogItem;
	@UiField
	Button btnSave;
	
	@UiHandler("btnSave")
	void onClick(ClickEvent e) {
		BacklogItem backlogItem=new BacklogItem();
		backlogItem.setBacklogItemName(txtBacklogItem.getText());
		project.setBacklogItem(backlogItem);
		txtBacklogItem.setText("");
		try {
			ControllerImpl.getController().changeView(View.PROJECTBACKLOG, project.getId().toString(), null);
		} catch (Exception ex) {
			// TODO Auto-generated catch block
		}	
	}
	
	
	public BacklogPanel(){
		initWidget(uiBinder.createAndBindUi(this));
		try {
			ProjectStore projectStore=ProjectStore.getProjectStore();
			// id van project verkrijgen van navigator 
			project = projectStore.getProjectById(Long.parseLong(Navigator.getNavigator().getProjectId()));
			for(BacklogItem backlogItem:project.getBacklogItems()){
				BacklogWidget backlogWidget = new BacklogWidget(backlogItem, project.getBacklogItems(),project);
				verticalPanel.add(backlogWidget);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	
	
	

}
